package cn.com.suiyi.shixingshengxian.home;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.youth.banner.Banner;



import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cn.com.suiyi.shixingshengxian.R;

import cn.com.suiyi.shixingshengxian.commoditydetail.CommodityActivity;
import cn.com.suiyi.shixingshengxian.home.adapter.FlashSaleAdapter;


import cn.com.suiyi.shixingshengxian.home.adapter.HomeTestAdapter;
import cn.com.suiyi.shixingshengxian.home.bean.Vegetable;

import cn.com.suiyi.shixingshengxian.home.myview.RecyclerViewHeader;

import cn.com.suiyi.shixingshengxian.home.myview.FullyLinearLayoutManager;



/**
 * Created by hansen on 2016/7/18.
 */

public class HomeFragment extends Fragment {

    @InjectView(R.id.home_recyVegetable)
    RecyclerView homeRecyVegetable;

    @InjectView(R.id.home_recyheader)
    cn.com.suiyi.shixingshengxian.home.myview.RecyclerViewHeader homeRecyheader;
    @InjectView(R.id.home_topBanner)
    Banner banner;
    @InjectView(R.id.home_hour)
    TextView homeHour;
    @InjectView(R.id.home_minute)
    TextView homeMinute;
    @InjectView(R.id.home_second)
    TextView homeSecond;
    @InjectView(R.id.home_flashSale)
    RecyclerView homeFlashSale;
    @InjectView(R.id.home_indent)
    ImageView homeIndent;
    @InjectView(R.id.home_return)
    ImageView homeReturn;
    @InjectView(R.id.home_checkin)
    ImageView homeCheckin;
    @InjectView(R.id.home_change)
    ImageView homeChange;
    @InjectView(R.id.home_new)
    ImageView homeNew;
    @InjectView(R.id.home_hot)
    ImageView homeHot;
    @InjectView(R.id.home_chef)
    ImageView homeChef;
    @InjectView(R.id.home_coupon)
    ImageView homeCoupon;

    @InjectView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;


    private List<Vegetable.DataBean> list;
    private HomeTestAdapter adapter;
    private HomeContract.HomePresenter presenter;
    private HomeContract.HomeModel model;
    private List<FlashSale.DataBean.FlashSaleBean> flashSaleList;
    private FlashSaleAdapter flashSaleAdapter;
    private List<HeardPic.DataBean> picList;
    List<String> imageslist;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_home, container, false);
        ButterKnife.inject(this, view);

        model = new HomeModel();
        presenter = new HomePresenter(model, this);

        presenter.getData();
        presenter.getFlashSale();
        presenter.getImage();

        //banner
        banner.setBannerStyle(Banner.INVISIBLE);
        banner.setIndicatorGravity(Banner.CENTER);
        banner.setDelayTime(4000);
        picList = new ArrayList<>();


        //限时抢购recyview
        LinearLayoutManager topmanager = new LinearLayoutManager(getActivity());
        topmanager.setOrientation(LinearLayoutManager.HORIZONTAL);
        homeFlashSale.setLayoutManager(topmanager);


        flashSaleList = new ArrayList<>();
        flashSaleAdapter = new FlashSaleAdapter(R.layout.fragment_home_flashsale, flashSaleList, getActivity());
        homeFlashSale.setAdapter(flashSaleAdapter);


        //下面recyview的管理
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());

        View view = inflater.inflate(R.layout.fragment_home, null);
        View topView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_hometop, null);
        ButterKnife.inject(this, view);

        List<Vegetable.DataBean> list = new ArrayList<>();



        //recyview的管理
        FullyLinearLayoutManager manager=new FullyLinearLayoutManager(getActivity());

        manager.setOrientation(LinearLayoutManager.VERTICAL);
        homeRecyVegetable.setLayoutManager(manager);

        //适配recyview
      //  HomeTopAdapter adapter = new HomeTopAdapter(R.layout.activity_homeitem,list,getActivity() );

        //添加头部

        homeRecyheader.attachTo(homeRecyVegetable);
        //适配数据
        list = new ArrayList<>();
        adapter = new HomeTestAdapter(getActivity(), list);
        adapter.setVegetableClick(new HomeTestAdapter.VegetableClick() {
            @Override
            public void setVegetableClick(View v, int position) {
                Log.i("tag","=======");
                Intent intent=new Intent(getActivity(), CommodityActivity.class);
                intent.putExtra("id",list.get(0).getFloorItem().get(position).getProductId());
                startActivity(intent);
            }
        });

     //   adapter.addHeaderView(topView);

        //测试adapter
        HomeTestAdapter adapter=new HomeTestAdapter(getActivity(),list);

        homeRecyVegetable.setAdapter(adapter);

//下拉刷新
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                swipeRefreshLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        homeRecyVegetable.setAdapter(adapter);
                        homeFlashSale.setAdapter(flashSaleAdapter);
                        swipeRefreshLayout.setRefreshing(false);
                    }
                },2000);
            }
        });
        //重写源码 增加回掉接口
        homeRecyheader.setOnScrollTopListener(new RecyclerViewHeader.OnScrollTopListener() {
            @Override
            public void onTop(boolean isTop) {
                swipeRefreshLayout.setEnabled(isTop);
            }
        },homeRecyVegetable);

        return view;
    }



    @Override
    public void onData(Vegetable vegetable) {
        //Log.i("tag", "-------->>>>>" + vegetable.getData().get());
        list.addAll(vegetable.getData());
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onFail(String string) {

    }

    @Override
    public void onFlashSale(FlashSale flashSale) {
        flashSaleList.addAll(flashSale.getData().getFlashSale());
        flashSaleAdapter.notifyDataSetChanged();
    }

    @Override
    public void onImage(HeardPic pic) {
        picList.addAll(pic.getData());
        imageslist = new ArrayList<>();
        for (int i = 0; i < picList.size(); i++) {
            imageslist.add(picList.get(i).getImage());
          //  Log.i("tag", picList.get(i).getImage() + "");
        }
        //banner加载数据
        banner.setImages(imageslist, new Banner.OnLoadImageListener() {
            @Override
            public void OnLoadImage(ImageView view, Object url) {
                Glide.with(getActivity()).load(url).into(view);
            }
        });
    }



    @Override
    public void onDestroyView() {

        super.onDestroyView();
        ButterKnife.reset(this);
    }
}
